<template>
	<div>
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>系统用户管理</el-breadcrumb-item>
		</el-breadcrumb>
		<el-button type="text" class="zt" @click="findAll">全部用户</el-button>
		<el-button type="text" class="zt" @click="findForGL">管理员</el-button>
		<el-button type="text" class="zt" @click="findForZXS">咨询师</el-button>
		<el-button type="text" class="zt" @click="findForKF">客服</el-button>

		<el-form :inline="true" :model="queryInfo" style="margin-top: 10px;">
			<el-form-item label="用户名称">
				<el-input v-model="queryInfo.username"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="getsystemUserList()">搜索</el-button>
			</el-form-item>
		</el-form>

		<el-button type="primary" size="small" style="margin-bottom: 20px;" @click="addSystemUser()">新增人员</el-button>

		<el-table :data="systemUserList" style="width: 100%" border>
			<el-table-column prop="id" label="ID" width="180">
			</el-table-column>
			<el-table-column prop="username" label="名称" width="180">
			</el-table-column>
			<el-table-column prop="password" label="密码">
			</el-table-column>
			<el-table-column prop="role" label="用户类型">
			</el-table-column>
			<el-table-column prop="phone" label="电话">
			</el-table-column>
			<el-table-column prop="email" label="邮箱">
			</el-table-column>
			<el-table-column prop="img" label="头像">
				<template slot-scope='scope'>
					<el-image style="width: 100px; height: 100px"
						:src="'http://121.41.0.164:8888/sfzy/downLoad?filename='+scope.row.img"></el-image>
				</template>
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-link :underline="false" type="primary"
						@click="toEditSysyemUser(scope.row)">编辑用户</el-link>&nbsp;&nbsp;
					<el-link :underline="false" type="primary" @click="removeSystemUser(scope.row.id)">删除人员</el-link>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pageSize" :current-page="queryInfo.pageNum"
			@size-change="handleSizeChange" @current-change="handleCurrentChange"
			layout="total, sizes, prev, pager, next, jumper" :total="total" style="margin-top: 10px;">
		</el-pagination>




		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close='cancal()'>
			<el-form :model="systemUser">
				<el-form-item label="用户名" label-width="80px">
					<el-input v-model="systemUser.username" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" label-width="80px">
					<el-input v-model="systemUser.password" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" label-width="80px">
					<el-input v-model="systemUser.phone" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="人员类型" label-width="80px">
					<template>
						<el-radio v-model="systemUser.role" label="1">管理员</el-radio>
						<el-radio v-model="systemUser.role" label="2">咨询师</el-radio>
						<el-radio v-model="systemUser.role" label="3">客服</el-radio>
					</template>
					<!-- 	<el-input v-model="systemUser.role" autocomplete="off"></el-input> -->
				</el-form-item>

				<el-form-item label="用户头像" label-width="80px">
					<el-upload action="http://121.41.0.164:8888/sfzy/upload" list-type="picture-card"
						:on-success="handleSuccess" :limit="1" :on-preview="handlePictureCardPreview"
						:on-remove="handleRemove" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancal()">取 消</el-button>
				<el-button type="primary" @click="confirm()">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				queryInfo: {
					username: '',
					pageNum: 1,
					pageSize: 5,
					role: ''
				},
				systemUserList: [],
				total: 1,
				title: '',
				dialogFormVisible: false,
				systemUser: {
					"id": '',
					"username": "",
					"password": "",
					"nickName": "",
					"email": "",
					"phone": '',
					"img": '',
					"role": '',
				},
				fileList: [],
				dialogImageUrl: '', // 预览图片路径
				dialogVisible: false, // 预览图片的对话框
			}
		},
		methods: {
			findAll() {
				this.queryInfo = {
					username: '',
					pageNum: 1,
					pageSize: 5,
					role: ''
				}
				this.getsystemUserList()
			},
			findForKF() {
				this.queryInfo.role = '客服'
				this.getsystemUserList()
			},
			findForZXS() {
				this.queryInfo.role = '咨询师'
				this.getsystemUserList()
			},
			findForGL() {
				this.queryInfo.role = '管理员'
				this.getsystemUserList()
			},
			toEditSysyemUser(row) {
				//console.log(row)
				// 赋值
				this.systemUser = row;
				// 设置标题
				this.title = "修改用户";
				// 开启对话框
				this.dialogFormVisible = true;
				// 动态的获取图片的名称并帮到到fileList
				this.fileList = [{
					name: row.img,
					url: 'http://121.41.0.164:8888/sfzy/downLoad?filename=' + row.img
				}];
			},
			// 处理上传成功的函数
			handleSuccess(response) {
				this.systemUser.img = response.message;
				console.log(this.systemUser.img)
			},
			// 删除图片的事件
			handleRemove(file, fileList) {
				console.log(file, fileList);
				console.log(this.systemUser.img)
				// 移除数据
				this.systemUser.img = '';
				console.log(this.systemUser.img)
			},
			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			confirm() {
				if (this.systemUser.id) {
					this.$http.put('Systemuser', this.systemUser)
						.then(resa => {
							if (resa.data.status == 200) {
								this.$message.success('添加成功')
								this.getsystemUserList()
								console.log(this.systemUser.img)
								this.cancal()
							} else {
								this.$message.error('提交失败')
							}
						})
				} else {
					this.$http.post('Systemuser/insert', this.systemUser)
						.then(resa => {
							if (resa.data.status == 200) {
								this.$message.success('提交成功')
								this.getsystemUserList()
								this.cancal()
							} else {
								this.$message.error('提交失败')
							}

						})
				}

			},
			cancal() {
				this.dialogFormVisible = false;
				this.systemUser = {
					"id": '',
					"username": "",
					"password": "",
					"nickName": "",
					"email": "",
					"phone": '',
					"img": '',
					"role": '',
				}
				this.fileList = []
			},
			addSystemUser() {
				this.dialogFormVisible = true
				this.title = "新增人员"
			},
			removeSystemUser(id) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http.delete('Systemuser/delete/' + id)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success(res.data.message)
								this.getsystemUserList()
							} else {
								this.$message.error("删除失败")
							}
						})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			getsystemUserList() {
				this.$http.get('Systemuser/pageSelect', {
						params: this.queryInfo
					})
					.then(res => {
						if (res.data.status == 200) {
							this.systemUserList = res.data.data.list
							this.total = res.data.data.total
						} else {
							this.$message('请求异常')
						}
					})
					.catch(err => {
						this.$message.error(err)
					})
			},
			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.getsystemUserList()
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.getsystemUserList()
			}
		},
		mounted() {
			this.getsystemUserList()
		}
	}
</script>

<style>
	.zt {
		width: 80px;
		height: 70px;
		display: inline-block;
		padding: 0 10px;
		margin: 10px 0;
	}
</style>